package cn.pconline.autoclub.task.util;

import cn.pconline.autoclub.task.FileUtils;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

/**
 * MC工具
 *
 * @author <a href="mailto:chensy0203@163.com">chensy</a>
 */
public class McUtils {

    private final static MemCachedClient mcc = new MemCachedClient("autoclub");

    static {
        String serverStr = FileUtils.getConfig("memcached.servers");
        System.out.println("mcServer:" + serverStr);
        String[] servers = serverStr.split(",");
        int maxConn = FileUtils.getConfig("memcached.maxConn", 50);
        int initConn = FileUtils.getConfig("memcached.initConn", 10);
        int socketConnectTO = FileUtils.getConfig("memcached.socketConnectTO", 800);
        int socketTO = FileUtils.getConfig("memcached.socketTO", 500);
        int maintSleep = FileUtils.getConfig("memcached.maintSleep", 3000);
        boolean failover = FileUtils.getConfig("memcached.failover", false);
        boolean failback = FileUtils.getConfig("memcached.failback", false);
        boolean nagle = FileUtils.getConfig("memcached.nagle", false);

        SockIOPool mccSocketIOPool = SockIOPool.getInstance("autoclub");
        mccSocketIOPool.setServers(servers);
        mccSocketIOPool.setMaxConn(maxConn);
        mccSocketIOPool.setInitConn(initConn);
        mccSocketIOPool.setSocketConnectTO(socketConnectTO);
        mccSocketIOPool.setSocketTO(socketTO);
        mccSocketIOPool.setMaintSleep(maintSleep);
        mccSocketIOPool.setFailback(failback);
        mccSocketIOPool.setFailover(failover);
        mccSocketIOPool.setNagle(nagle);

        mccSocketIOPool.initialize();
        mcc.setSanitizeKeys(false);

    }

    public static MemCachedClient getMemCachedClient() {
        return mcc;
    }

    public static boolean delete(String key) {
        return mcc.delete(key);
    }

    public static void main(String[] args) {
        if (args != null && args.length == 2) {
            if ("delete".equals(args[0])) {
                System.out.println("delete:" + mcc.delete(args[1]));
            } else if ("get".equals(args[0])) {
                System.out.println("get:" + mcc.get(args[1]));
            }
        } else {
            System.out.println("please input [action(delete|get) key]");
        }
    }
}